package com.sparkseries.service.impl;

import com.sparkseries.common.util.domain.Result;
import com.sparkseries.service.LoginService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Resource
    private AuthenticationManager authenticationManager;
    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return 默认响应类
     */
    @Override
    public Result<?> login(String username, String password) {
        try {
            Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
            return Result.ok("登录成功");
        } catch (AuthenticationException e) {
            log.warn("用户[{}]登录失败，原因：{}", username, e.getMessage());
            return Result.error("用户名或密码错误");
        }
    }

    /**
     * TODO 二次验证（待实现）
     * @return 默认响应类
     */
    @Override
    public Result<?> secondVerify() {
        // 待实现逻辑（例如发送验证码或调用身份验证服务）
        return Result.ok("检验成功");
    }

}
